//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics.Contracts;
using System.Windows.Documents;
using OpenLS.Core.Serialization;

namespace OpenLS.Drawing
{
    public class RegularTextRun : RunBase
    {
        private string text = "";
        private TextCharacterProperties textCharacterProperties;

        public RegularTextRun()
        {
        }

        public RegularTextRun(string text)
        {
            Text = text;
        }

        public TextCharacterProperties TextCharacterProperties
        {
            get { return textCharacterProperties; }
            set { textCharacterProperties = value; }
        }


        public string Text
        {
            get { return text; }
            set { text = value; }
        }


        internal override Inline Inline
        {
            get
            {
                Run result = new Run();
                result.Text = Text;
                if (TextCharacterProperties != null)
                    TextCharacterProperties.ApplyTo(result);
                return result;
            }
        }

        internal override void ReadXmlCore(ReadContext context)
        {
            using (var c = context.Read("r"))
            {
                TextCharacterProperties = c.ReadOptionalElementAs<TextCharacterProperties>("rPr");
                using (ReadContext cc = context.Read("t"))
                {
                    cc.PassAttributes();
                    Text = cc.ReadString();
                }
            }
        }

        internal override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write("r"))
            {
                c.WriteOptionalElementAs(TextCharacterProperties, "rPr");
                using (WriteContext cc = c.Write("t"))
                {
                    cc.Writer.WriteString(Text);
                }
            }
        }

        internal TextCharacterProperties EnsureTextCharacterProperties()
        {
            Contract.Ensures(Contract.Result<TextCharacterProperties>() != null);
            if (TextCharacterProperties == null)
                TextCharacterProperties = new TextCharacterProperties();
            return TextCharacterProperties;
        }
    }
}